package me.fung.rem.app.content.card

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.PointerMatcher
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.onClick
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.PointerButton
import androidx.compose.ui.unit.dp
import me.fung.keen.common.TextClipboard
import me.fung.keen.component.message.localMessage
import me.fung.rem.app.content.ContentView
import me.fung.rem.app.navigation
import me.fung.rem.app.rem_editor.PAGE_REM_EDITOR
import me.fung.rem.bean.repo.RemItem
import me.fung.rem.bean.repo.tags

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ContentView.rememberItemCard(
    item: RemItem,
) {
    val message = localMessage.current
    card(Modifier) {
        Column(
            modifier = Modifier.fillMaxSize().padding(top = 10.dp)
                .onClick(matcher = PointerMatcher.mouse(PointerButton.Primary)) {
                    TextClipboard.copy(item.password)
                    message.send { info("已复制密码") }
                    p.saveFilterSearch()
                }
                .onClick(matcher = PointerMatcher.mouse(PointerButton.Secondary)) {
                    navigation.goto(PAGE_REM_EDITOR, listOf(item))
                    p.saveFilterSearch()
                },
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Spacer(Modifier.weight(1f))
            Text(item.title)
            Spacer(Modifier.size(4.dp).weight(1f))
            Text(item.username)
            Spacer(Modifier.size(4.dp))
            Text(item.info, modifier = Modifier.padding(horizontal = 8.dp))
            Spacer(Modifier.weight(1f))
            remItemTag(item, modifier = Modifier.align(Alignment.Start).padding(bottom = 1.dp))
        }
    }

}

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun remItemTag(
    item: RemItem,
    modifier: Modifier
) {
    FlowRow(
        modifier = modifier
    ) {
        item.tags().forEach {
            Text(
                text = it,
                modifier = Modifier.padding(horizontal = 1.dp)
                    .border(1.dp, color = Color.LightGray, shape = RoundedCornerShape(20))
                    .padding(2.dp)
                    .height(15.dp),
                color = Color.LightGray,
            )
        }
    }
}